<!--基本柱状图-->

<template>
  <div class="w-h-full">
    <div v-if="shade" class="shade"></div>
    <div v-resize="resizeHandle" style="width: 100%; height: 100%;">
      <div :id="'chart-'+component.id+'-'+chartDomNum" class="w-h-full"></div>
    </div>
  </div>
</template>

<script>

  import FuncCompMixin from '@/mixin/FuncCompMixin'
  import ChartCompMixin from '@/mixin/ChartCompMixin'
  import PnDesigner from '@/utils/PnDesigner'

  import BaseBarChart from './BaseBarChart'

  const _this = {
    name: 'BaseBarChart',
    mixins: [FuncCompMixin, ChartCompMixin],
    attr: {
      version: '2.4',
      configDataTemp: Object.assign({

        chartOption: {
          title: PnDesigner.buildTitleConfigData(),
          grid: PnDesigner.buildGridConfigData(),
          legend: PnDesigner.buildLegendConfigData({
            show: false
          }),
          tooltip: PnDesigner.buildTooltipConfigData({
            axisPointer: {
              type: 'shadow'
            }
          }),
          xAxis: PnDesigner.buildXAxisConfigData(),
          yAxis: PnDesigner.buildYAxisConfigData({
            name: '数量',
            show: false
          }),
          visualMap: [],
          series: [
            BaseBarChart.buildSeriesObj('系列1')
          ]
        },

      }, PnDesigner.buildFuncCompDatasourceField({
        ds_resultObjTemplate: [
          {
            field: 'name',
            remark: '类目'
          },
          {
            field: 'value',
            remark: '值'
          },
          {
            field: 'series',
            remark: '系列'
          }
        ],
        ds_resultObj: [
          {
            'name': '南宁',
            'value': 10,
            'series': '系列1'
          },
          {
            'name': '柳州',
            'value': 52,
            'series': '系列1'
          },
          {
            'name': '桂林',
            'value': 200,
            'series': '系列1'
          },
          {
            'name': '崇左',
            'value': 334,
            'series': '系列1'
          },
          {
            'name': '北海',
            'value': 390,
            'series': '系列1'
          },
          {
            'name': '防城港',
            'value': 330,
            'series': '系列1'
          },
          {
            'name': '玉林',
            'value': 220,
            'series': '系列1'
          }
        ],
        ds_useFilter: true,
        ds_useTimer: true,
      }), PnDesigner.buildFuncCompGlobalField(), PnDesigner.buildEchartCompCustomOption(), PnDesigner.buildFuncCompInteractionConfiguration([
          PnDesigner.buildFuncCompInteractionStreamItem('itemClick', [
              PnDesigner.buildFuncCompInteractionFieldsMapItem('name', '', '类目'),
              PnDesigner.buildFuncCompInteractionFieldsMapItem('value', '', '值'),
              PnDesigner.buildFuncCompInteractionFieldsMapItem('series', '', '系列'),
          ])
      ]))
    },
    data() {
      return {}
    },
    mounted() {

      this.init();

      this.setTimer(() => {
        this.init()
      })
    },
    methods: {
      init() {
        this.initDatasource(() => {
          this.chart.setOption(this.chartOption);
          this.runCustomGlobalJsCode();
          this.runDataItemClickJsCode(this.launchInteractionStream)
        }, (result) => {
          this.chart.setOption(this.chartOption);
          this.runCustomGlobalJsCode();
          this.runDataItemClickJsCode(this.launchInteractionStream)
        });
      },

      launchInteractionStream (params) {
        this.$PnDesigner.launchInteractionStream(this.component.compConfigData, 'itemClick', params.data)
      }
    },
    computed: {
      chartOption() {

        let option = {
          title: PnDesigner.buildTitleChartOption(this.component.compConfigData.chartOption.title),
          grid: PnDesigner.buildGridChartOption(this.component.compConfigData.chartOption.grid),
          legend: PnDesigner.buildLegendChartOption(this.component.compConfigData.chartOption.legend),
          tooltip: PnDesigner.buildTooltipChartOption(this.component.compConfigData.chartOption.tooltip),
          xAxis: [
            PnDesigner.buildXAxisChartOption(this.component.compConfigData.chartOption.xAxis)
          ],
          yAxis: [
            PnDesigner.buildYAxisChartOption(this.component.compConfigData.chartOption.yAxis)
          ],
          visualMap: PnDesigner.buildVisualMapChartOption(this.component.compConfigData.chartOption.visualMap),
          series: []
        };

        // 初始化横坐标标签
        this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'name').forEach(item => {
          option.xAxis[0].data.push(item.type)
        });

        /*this.component.compConfigData.chartOption.series.forEach((item, index) => {
          let groupItem = this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'series')[index];
          if (groupItem) {
            option.series.push({
              name: item.name,
              type: 'bar',
              label: {
                show: item.label.show,
                position: item.label.position,
                color: item.label.color,
                fontSize: item.label.fontSize
              },
              itemStyle: {
                borderWidth: item.itemStyle.borderWidth,
                borderColor: item.itemStyle.borderColor,
                barBorderRadius: item.itemStyle.barBorderRadius ? item.itemStyle.barBorderRadius : 0,
                color: item.itemStyle.color ? item.itemStyle.color : new this.$Echarts.graphic.LinearGradient(
                  0, 0, 0, 1, [
                    {
                      offset: 1,
                      color: item.itemStyle.startColor ? item.itemStyle.startColor : this.currentEchartThemeColors[0],
                    },
                    {
                      offset: 0,
                      color: item.itemStyle.endColor ? item.itemStyle.endColor : this.currentEchartThemeColors[7]
                    }
                  ]
                )
              },
              data: groupItem.list,
            });
          }
          option.legend.data.push(item.name)
        });*/

        this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'series').forEach((resultObjItem, index) => {
          if (resultObjItem) {
            let item = this.component.compConfigData.chartOption.series[index];
            if (item) {
              option.series.push({
                name: item.name ? item.name : resultObjItem.type,
                type: item.type,
                showBackground: item.showBackground,
                backgroundStyle: {
                  color: item.backgroundStyle.color,
                  borderColor: item.backgroundStyle.borderColor,
                  borderWidth: item.backgroundStyle.borderWidth,
                  borderType: item.backgroundStyle.borderType,
                  borderRadius: item.backgroundStyle.borderRadius,
                  shadowBlur: item.backgroundStyle.shadowBlur,
                  shadowColor: item.backgroundStyle.shadowColor
                },
                label: {
                  show: item.label.show,
                  position: item.label.position,
                  rotate: item.label.rotate,
                  formatter: item.label.formatter,
                  color: item.label.color,
                  fontSize: item.label.fontSize
                },
                itemStyle: {
                  color: item.itemStyle.color ? item.itemStyle.color : new this.$Echarts.graphic.LinearGradient(
                      0, 0, 0, 1, [
                        {
                          offset: 0,
                          color: item.itemStyle.startColor ? item.itemStyle.startColor : this.currentEchartThemeColors[7],
                        },
                        {
                          offset: 1,
                          color: item.itemStyle.endColor ? item.itemStyle.endColor : this.currentEchartThemeColors[0]
                        }
                      ]
                  ),
                  borderColor: item.itemStyle.borderColor,
                  borderWidth: item.itemStyle.borderWidth,
                  borderType: item.itemStyle.borderType,
                  barBorderRadius: item.itemStyle.barBorderRadius,
                  shadowBlur: item.itemStyle.shadowBlur,
                  shadowColor: item.itemStyle.shadowColor
                },
                barWidth: item.barWidth,
                barGap: item.barGap,
                barCategoryGap: item.barCategoryGap,
                markLine: PnDesigner.buildMarkLineChartOption(item.markLine),
                markPoint: PnDesigner.buildMarkPointChartOption(item.markPoint),
                data: resultObjItem.list,
              });
              option.legend.data.push(item.name ? item.name : resultObjItem.type)
            }else {
              option.series.push(BaseBarChart.buildDefaultSeriesObj(resultObjItem.type, resultObjItem.list));
              option.legend.data.push(resultObjItem.type)
            }
          }
        })

        // 下面这一段代码用于合并组件的扩展配置，如果组件不需要扩展配置，可以删掉
        option = this.$PnDesigner.mergeChartCustomOption(option, this.component.compConfigData.customOption);

        return option;
      }
    },
    watch: {}
  };

  export default _this
</script>

<style scoped>

</style>
